using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;

namespace CMS.Web
{
    /// <summary>
    /// A class that helps to build download buttons/links on the fly
    /// </summary>
    static public class DownloadAssistant
    {
        //the path of uploaded files in the system
        static public string UploadPath = System.Configuration.ConfigurationManager.AppSettings["UploadPath"];

        static public void DownloadFile(string fileName)
        {
            try
            {
                if (HttpContext.Current.Session["UploadPath"] != null)
                    UploadPath = HttpContext.Current.Session["UploadPath"].ToString();
                else
                {
                    UploadPath = System.Configuration.ConfigurationManager.AppSettings["UploadPath"];
                }
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(fileName));
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.WriteFile(UploadPath + fileName);
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        static public void DownloadFile(string fileName, string pathFolder)
        {
            try
            {
                if (HttpContext.Current.Session["UploadPath"] != null)
                    UploadPath = HttpContext.Current.Session["UploadPath"].ToString();
                else
                {
                    UploadPath = System.Configuration.ConfigurationManager.AppSettings["UploadPath"];
                }
                UploadPath = Path.Combine(UploadPath, pathFolder);
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(fileName));
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.WriteFile(UploadPath + "/" + fileName);
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        /// <summary>
        /// This method is used to export to files based on crystal report
        /// </summary>
        /// <param name="reportDocument">ReportDocument object</param>
        /// <param name="strExportFile">file name in general(not include extension files)</param>
        /// <param name="fileExtension">file extension</param>
        static public void ExportAsStream(ReportDocument reportDocument, string strExportFile, string fileExtension)
        {

            ExportOptions objExOpt = default(ExportOptions);
            objExOpt = reportDocument.ExportOptions;
            switch (fileExtension.ToLower())
            {
                case "pdf":
                    objExOpt.ExportFormatType = ExportFormatType.PortableDocFormat;
                    break;
                case "doc":
                    objExOpt.ExportFormatType = ExportFormatType.WordForWindows;
                    break;
                case "xls":
                    objExOpt.ExportFormatType = ExportFormatType.Excel;
                    break;
            }
            reportDocument.ExportToHttpResponse(objExOpt, HttpContext.Current.Response, true, strExportFile);
        }
    }
}
